<?php


use yii\bootstrap\ActiveForm;
use yii\widgets\Pjax;
use yii\jui\Widget;
use yii\jui\DatePicker;
use backend\models\Country;
use backend\models\Categories;
use kartik\grid\GridView;
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model backend\models\Customer */
/* @var $form yii\widgets\ActiveForm */
?>
<!--<script>-->
<?php
$js = <<<JS
    $(function () {
        $('form').find('checkbox,radio,textarea').each(function () {
            $(this).wrap("<div class='col-sm-10'></div>")
        })
        $('.form-group > div').each(function () {
            $(this).addClass('col-sm-10')
        })

        $('.help-block').each(function () {
            $(this).addClass('col-sm-offset-2')
        })

        $('.form-group > input[type!=hidden]').each(function () {
            $(this).wrap("<div class='col-sm-10'></div>")
        })

        $('.form-group > label').each(function () {
            $(this).addClass('col-sm-2')
        });
    });
    $(".radio").addClass("radio-inline")
    
   
JS;

$js_checkbox = <<<CHECKBOX

$("#customer-province .checkbox").addClass("checkbox-inline")
$("#customer-project_province .radio").addClass("checkbox-inline")

CHECKBOX;

$js_check_company = <<<CHECK_COMPANY
$('#customer-company').blur(function(){
url = $('#chk-company-unique').val()
$.post(url,{"company":$(this).val()},function(data){
if(data == true){
 $('#customer-company').next().html('机构名称已录过系统，请确认是否录入')
 $('#customer-company').next().show()
}else{

}
});
});
CHECK_COMPANY;

$js_contact = <<<CONTACT
function addRow(item) {
    var html_tr ='<tr data-key="1">'+
    '<td class="skip-export kv-align-center kv-align-middle kv-row-select" style="width:50px;" data-col-seq="0"><input class="kv-row-checkbox" name="selection[]" value="1" type="checkbox"></td>'+
    '<td data-col-seq="1"><input id="customercontact-contact" class="form-control" name="CustomerContact[][contact]" placeholder="联系人" aria-required="true" aria-invalid="true" type="text"></td>'+
    '<td data-col-seq="2"><input id="customercontact-post" class="form-control" name="CustomerContact[][post]" placeholder="职务" aria-required="true" aria-invalid="true" type="text"></td>'+
    '<td data-col-seq="3"><input id="customercontact-mobile" class="form-control" name="CustomerContact[][mobile]" placeholder="电话" aria-required="true" aria-invalid="true" type="text"></td>'+
    '<td data-col-seq="4"><input id="customercontact-image" name="CustomerContact[][image]" aria-invalid="false" type="file"></td>'+
    '<td><span class="btn btn-default" onclick="addRow($(this))">ADD</span></td></tr>'
    
    item.parent().parent().after(html_tr)
}
CONTACT;

$js_add_contact = <<<JS
function addNewRow(item) {
  var i=$("#counter").val()
  var html_contact ='<div class="row form-inline"><div class="col-sm-offset-2 col-sm-10"><div class="form-group field-customercontact-'+i+'-contact required"><div class="col-sm-6 col-sm-offset-3">' +
   '<input id="customercontact-'+i+'-contact" class="form-control" name="CustomerContact['+i+'][contact]" placeholder="联系人" type="text"><div class="help-block help-block-error "></div></div></div>'+
            '<div class="form-group field-customercontact-'+i+'-post required"><div class="col-sm-6 col-sm-offset-3">' +
             '<input id="customercontact-'+i+'-post" class="form-control" name="CustomerContact['+i+'][post]" placeholder="职务" type="text"><div class="help-block help-block-error "></div></div></div>' +
              '<div class="form-group field-customercontact-'+i+'-mobile required"><div class="col-sm-6 col-sm-offset-3">' +
               '<input id="customercontact-'+i+'-mobile" class="form-control" name="CustomerContact['+i+'][mobile]" placeholder="电话" type="text"><div class="help-block help-block-error "></div></div></div>' +
                '<div class="form-group field-customercontact-'+i+'-image"><div class="col-sm-6 col-sm-offset-3">' +
                 '<input name="CustomerContact['+i+'][image]" value="" type="hidden"><input id="customercontact-'+i+'-image" name="CustomerContact['+i+'][image]" placeholder="名片" type="file"><div class="help-block help-block-error "></div></div></div>' +
                  '<label class="btn btn-info" onclick="addNewRow($(this))">继续添加</label></div></div>'
  item.parent().parent().after(html_contact)
  $('#counter').val(parseInt(i)+1)
}
JS;


$this->registerJs($js_check_company, \yii\web\View::POS_END);
$this->registerJs($js_checkbox, \yii\web\View::POS_END);
$this->registerJs($js_add_contact, \yii\web\View::POS_END);
?>

<div class="customer-form">

    <?php
    //    Pjax::begin([
    //        //Pjax options
    //    ]);
    echo "<input type='hidden' id='counter' value='1' />";
    echo "<input type='hidden' id='chk-company-unique' value='" . \yii\helpers\Url::to(['customer/company-unique']) . "'>";

    $form = ActiveForm::begin([
        'layout' => 'horizontal',
        'options' => ['enctype' => 'multipart/form-data', 'data' => ['pjax' => false],]
    ]); ?>

    <div class="col-md-offset-2">
        <h4 class="text-success ">基础信息</h4>
    </div>
    <hr>
    <?= $form->field($model, 'url')->textInput(['maxlength' => true,]) ?>

    <?php // echo $form->field($model, 'company', ['enableAjaxValidation' => true])->textInput(['maxlength' => true,]);//AJAX 验证， ?>
    <?= $form->field($model, 'company')->textInput(['maxlength' => true,]) ?>
    <?= $form->field($model, 'company_province')->dropDownList(Country::localCity('`pid`=0 AND `order`>0'), [
        'prompt' => '选择省份',
        'onchange' => '
        $.post("' . Yii::$app->urlManager->createUrl('country/city') . '&typeid=1&pid="+$(this).val(),function(data){$("#customer-company_city").html(data);});',
    ]) ?>
    <?= $form->field($model, 'company_city')->dropDownList(Country::getCityList($model->company_province), ['prompt' => '选择城市']) ?>
    <?= $form->field($model, 'company_type')->radioList(Yii::$app->params['company_type']) ?>
    <?= $form->field($model, 'company_level')->radioList(Yii::$app->params['company_level']) ?>

    <div class="col-md-offset-2" id="contact-info">
        <h4 class="text-success ">联系人信息</h4>
    </div>
    <?php echo $this->render('contact-list', ['contactDataProvider' => $contactDataProvider]) ?>
    <div class="row form-inline">
        <div class="col-sm-offset-2 col-sm-10">
            <?= $form->field($contactModel, '[0]contact')->textInput(['placeholder' => '联系人'])->label(false) ?>
            <?= $form->field($contactModel, '[0]post')->textInput(['placeholder' => '职务'])->label(false) ?>
            <?= $form->field($contactModel, '[0]mobile')->textInput(['placeholder' => '电话'])->label(false) ?>
            <?= $form->field($contactModel, '[0]image')->fileInput(['placeholder' => '名片', 'maxlength' => '10'])->label(false) ?>
            <label class="btn btn-info" onclick="addNewRow($(this))">继续添加</label>
        </div>
    </div>

    <div class="col-md-offset-2">
        <h4 class="text-success ">客户信息</h4>
    </div>
    <hr>
    <div class="form-group field-customer-type_desc">
        <label class="control-label col-sm-3">客户细分</label>
        <div class="col-sm-8">
            <input name="Customer[type_desc]" value="" type="hidden">
            <?php
            $arr_type_desc = Yii::$app->params['type_desc'];
            $arr_type_desc_group = [array_slice($arr_type_desc, 0, 5, true), array_slice($arr_type_desc, 5, 6, true), array_slice($arr_type_desc, 11, 8, true), array_slice($arr_type_desc, 19, 5, true)];
            array_walk($arr_type_desc_group, function ($value, $key) use ($model) {
                echo $key > 1 || $key == 0 ? '<div id="customer-type_desc" class="col-sm-3">' : '<div id="customer-type_desc" class="col-sm-2">';
                foreach ($value as $k => $item):
                    echo '<div class="checkbox"><label><input type="checkbox" name="Customer[type_desc][]" value="' . $k . '"';
                    if (is_array($model->type_desc) && in_array($k, $model->type_desc)): echo 'checked'; endif;
                    echo '/>' . $item . '</label></div>';
                endforeach;
                echo '</div>';
            });
            ?>
        </div>
        <div class="help-block help-block-error "></div>
    </div>


    <?php
    echo "<div class='form-group'><label  class='control-label col-sm-offset-1 col-sm-2' >业务范围</label></div>";
    $arr_province = Country::localCity('`pid`=0 AND `order`>0');

    $arr_province_group = ['全国' => array_slice($arr_province, 31, 1, true), '华北' => array_slice($arr_province, 0, 3, true), '华东' => array_slice($arr_province, 3, 6, true), '中南' => array_slice($arr_province, 9, 6, true),
        '西南' => array_slice($arr_province, 15, 6, treu), '东北' => array_slice($arr_province, 21, 3, true), '西北' => array_slice($arr_province, 24, 7, true)];

    array_walk($arr_province_group, function ($value, $key) use ($model) {
        echo '<div class="form-group" style="display: block;"><label class="control-label col-sm-offset-2 col-sm-2" >' . $key . '</label><div class="col-sm-6"><div>';
        foreach ($value as $k => $v) {
            echo '<div class="checkbox-inline"><label><input name="Customer[province][]" value="' . $k . '" type="checkbox"  ';
            if (is_array($model->province) && in_array($k, $model->province)) {
                echo ' checked=checked ';
            }
            echo '>' . $v . '</label></div>';
        }
        echo '</div></div><div class="help-block help-block-error "></div></div>';
    });
    ?>
    <div class="col-md-offset-2">
        <h4 class="text-success ">资产包信息</h4>
    </div>
    <hr>
    <?= $form->field($model, 'principal')->textInput(['maxlength' => true,]) ?>
    <?php echo $form->field($model, 'package_type')->radioList(Yii::$app->params['package_type']); ?>
    <?= $form->field($model, 'package_transfer')->radioList(Yii::$app->params['package_transfer']) ?>

    <?php
    echo "<div class='form-group'><label  class='control-label col-sm-offset-1 col-sm-2' >覆盖地区</label></div>";
    array_walk($arr_province_group, function ($value, $key) use ($model) {
        echo '<div class="form-group" style="display: block;"><label class="control-label col-sm-offset-2 col-sm-2" >' . $key . '</label><div class="col-sm-6"><div>';
        foreach ($value as $k => $v) {
            echo '<div class="checkbox-inline"><label><input name="Customer[project_province][]" value="' . $k . '" type="checkbox"  ';
            if (is_array($model->project_province) && in_array($k, $model->project_province)) {
                echo ' checked=checked ';
            }
            echo '>' . $v . '</label></div>';
        }
        echo '</div></div><div class="help-block help-block-error "></div></div>';
    });
    ?>

    <?php
    if ($model->project_file) {
        echo "<div class='col-sm-offset-2 col-sm-10'><ul class='list-group'><li class='list-group-item'>已上传的文件：</li>";
        $arr_file = explode('#', $model->project_file);
        echo "<li class='list-group-item'>" . Html::a($arr_file[1], $model->project_file) . '</li>';
        echo "</ul></div>";
    }
    ?>
    <?php //echo $form->field($model, 'project_file')->fileInput([]); ?>

    <div class="col-md-offset-2" id="history-info">
        <h4 class="text-success ">沟通信息</h4>
    </div>
    <hr>
    <?php echo $this->render('history-list', [
        'historyDataProvider' => $historyDataProvider,

    ]) ?>
    <?= $form->field($historyModel, 'history_date')->widget(DatePicker::classname(), ['dateFormat' => 'yyyy-MM-dd', 'inline' => false]) ?>
    <?= $form->field($historyModel, 'history_contact')->textInput() ?>
    <?= $form->field($historyModel, 'history_result')->textarea(['rows' => 2]) ?>

    <?php
    if (Yii::$app->user->identity->superadmin == 1): ?>
        <hr>
        <?= $form->field($model, 'review_level')->radioList(Yii::$app->params['review_level']) ?>
    <?php endif; ?>
    <hr>
    <?= $form->field($model, 'remark')->textarea(['rows' => 2,]) ?>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <?= Html::submitButton($model->isNewRecord ? '保存' : '提交', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        </div>
    </div>
    <?php
    ActiveForm::end();
    //    Pjax::end();

    ?>

</div>
